package com.neteast.video.activity;

import com.neteast.video.R;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;

public class SettingActivity extends Activity implements OnClickListener {
	private Button mSaveButton;
	private Button mClearButton;
	private AutoCompleteTextView mACTV;
	private SharedPreferences mSP;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_setting);
		init();
	}

	private void init() {
		mSP = getSharedPreferences("videoConfig",Context.MODE_PRIVATE);
		mSaveButton = (Button) this.findViewById(R.id.save);
		mClearButton = (Button) this.findViewById(R.id.clear);
		mACTV = (AutoCompleteTextView) this.findViewById(R.id.urlPath);
		mACTV.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,mUrl));
		
		String url = mSP.getString("url", MainActivity.IP);
		mACTV.setText(url);
		mSaveButton.setOnClickListener(this);
		mClearButton.setOnClickListener(this);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.save:
			String url = mACTV.getText().toString();
			if (url==null||"".equals(url)) {
				Toast.makeText(this, "网址不能为空", Toast.LENGTH_SHORT).show();
				return;
			}
			Editor editor = mSP.edit();
			editor.putString("url", url);
			editor.commit();
			this.setResult(RESULT_OK);
			this.finish();
			break;
		case R.id.clear:
			mACTV.setText("");
			break;
		}
	}
	
	public static final String[] mUrl=new String[]{
			MainActivity.IP};
}
